package com.llh.controller.http;

import com.llh.bean.Express;
import com.llh.bean.FinaIndicator;
import com.llh.bean.MiddleData;
import com.llh.controller.http.common.CommonExcuteTrade;
import com.llh.mapper.ExpressMapper;
import com.llh.mapper.FinaIndicatorMapper;
import com.llh.util.SqlSessionUtil;
import lombok.Data;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.List;
@Data
public class ExpressTrade extends CommonExcuteTrade {
    private MiddleData middleData;
    static Logger logger = LogManager.getLogger(CommonExcuteTrade.class.getName());
    @Override
    public void parseParam(String[][] items, String[] fields) throws Exception {
        //获取sqlSession
        SqlSession sqlSesion = null;

        //该接口只能一次调一只股票的,这里我们根据查询条件只会返回一条数据
        List<Express> list = ( List<Express>)copyProperty(items, fields);
            /*if(list.size()!=1){
                throw new Exception("数据有误");
            }*/
        if(list==null||list.size()==0){
            logger.error("没有获取到任何数据");
            return;
        }
        Express express = list.get(0);
        express.setOpen_day(middleData.getOpen_day());
        try {
            sqlSesion = SqlSessionUtil.getSqlSesion();
            ExpressMapper mapper = sqlSesion.getMapper(ExpressMapper.class);
            logger.info("插入数据");
            mapper.insertSelective(express);
            logger.info("插入操作成功");

        }catch (Exception e){
            e.printStackTrace();
            throw  new Exception("将数据插入到express失败");
        }
        finally {
            sqlSesion.close();
        }

    }

    @Override
    public List copyProperty(String[][] items, String[] fields) throws Exception {
        List<Express> expresses = new LinkedList<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        if(items==null||items.length==0){
            //没有数据则返回空list
            return expresses;
        }
        for (int j = 0; j < items.length; j++) {
            //参数值数组
            String[] item = items[j];
            if(item==null||item.length==0){
                //如果有数据组为null,为空数组,跳下一次循环
                continue;
            }
            Express express = new Express();
            Field[] expressFields = express.getClass().getDeclaredFields();

            //有效数据数据格式校验
            if(item.length!=fields.length){
                throw new Exception("响应的数据有误");
            }
            for (int i = 0; i < fields.length; i++) {
                //参数名
                String field = fields[i];
                //参数值
                String value = item[i];
                for (Field declaredField : expressFields) {
                    declaredField.setAccessible(true);
                    String fieldName = declaredField.getName();
                    if(field.equals(fieldName)){
                        //如果返回来的值为null,字符串"null",空字符串"",将对应参数设置为null
                        if(value==null||"null".equals(value.toLowerCase())||"".equals(value)){
                            declaredField.set(express,null);
                        }else{
                            //若果是正常值,则转换为对应的类型
                            if(field.equals("ann_date")||field.equals("end_date")){
                                //将字符串转换成date.并赋值
                                declaredField.set(express,format.parse(value));
                            }else if(field.equals("yoy_sales")||field.equals("yoy_dedu_np")){
                                //将字符串转换成double
                                declaredField.set(express,Double.valueOf(value));
                            }else{
                                //直接保留原字符串格式
                                declaredField.set(express,value);
                            }
                        }


                    }
                }
            }
            //添加到list集合
            expresses.add(express);

        }
        return expresses;
    }
}
